<?php

class phpinchains_ValidationErrors
{

    var $msgs = array();
    var $form;
    var $count = 1;
     
    function setForm ($form)
    {
        $this->form = $form;
    }
    
    function form()
    {
        return $this->form;
    }

    function addField($field)
    {
        $this->msgs[$field] = $this->count++;
    }
    
    function isError($field)
    {
        return isset($this->msgs[$field]);
    }
    
    function getAll()
    {
        return $this->msgs;
    }
    
    /**
     * To anable access for EL
     * 
     * Std validation errors:
     * validationErrors->flip
     * validationErrors->all
     * validationErrors->isError-><fieldname>
     */
    function get($key)
    {
        switch($key)
        {
            case 'flip':
                return array_flip($this->msgs);
            case 'isError':
                return $this->msgs;
            case 'hasErrors':
                return $this->hasErrors();
            case 'form':
                return $this->form();
            case 'dump':
                return join(', ', array_flip($this->msgs));
        }
    }
    
    function hasErrors()
    {
        return (count($this->msgs)>0);
    }
    
    function clear()
    {
        $this->form = '';
        unset($this->msgs);
        $this->msgs = array();
    }

}

?>